import { validNumberLetter, validNumberLetterCharacter, validEmail, validPhone, validTelephone, validQQ, validWX, validIDCard, validName, validUSCC, validUserName} from '@/utils/validate'
class formValidate {
  static validate({ type = 'null', blank = false, require = true }) {
    return (rule, value, callback) => {
      let reg
      if (blank) {
        if (value.trim() === '') {
          return callback(new Error('输入不能为纯空格'))
        }
      }
      if (!require) {
        if (!value || value.length === 0) {
          callback()
        }
      }
      switch (type) {
        case 'null' : reg = true; break
        case 'NumberLetter' : reg = validNumberLetter(value); break
        case 'NumberLetterCharacter' : reg = validNumberLetterCharacter(value); break
        case 'Email' : reg = validEmail(value); break
        case 'Phone' : reg = validPhone(value); break
        case 'userName' : reg = validUserName(value); break
        case 'Telephone' : reg = validTelephone(value); break
        case 'QQ' : reg = validQQ(value); break
        case 'WX' : reg = validWX(value); break
        case 'IDCard': reg = validIDCard(value); break
        case 'ChineseName': reg = validName(value); break
        case 'USCC': reg = validUSCC(value); break
      }
      if (!reg) {
        return callback(new Error('请输入合法的信息'))
      }
      callback()
    }
  }
  static isNumberLetter() { // 字母或者数字
    return (rule, value, callback) => {
      if (!value && value.trim() === '') {
        callback()
      }
      if (!validNumberLetter(value)) {
        return callback(new Error())
      }
      callback()
    }
  }
}

export default formValidate
